﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using Microsoft.Phone.Tasks;
using WCYCReader.EncodingHelper;
using WCYCReader.Main;
using WCYCReader.ShareFolder;
using WCYCReader.StorageHelper;

namespace WCYCReader.Pages
{
    public partial class ViewImage : PhoneApplicationPage
    {
        public ViewImage()
        {
            InitializeComponent();

            this.webBrowser.Navigated += webBrowser_Navigated;
            this.webBrowser.NavigationFailed += webBrowser_NavigationFailed;
        }

        void webBrowser_Navigated(object sender, NavigationEventArgs e)
        {

        }

        void webBrowser_NavigationFailed(object sender, NavigationFailedEventArgs e)
        {
            Prompt.PromptMessage("加载文件内容失败！");
        }


        protected override void OnNavigatedTo(NavigationEventArgs navigationEventArgs_0)
        {
            //string item = base.NavigationContext.QueryString["filename"];
            //if (!string.IsNullOrWhiteSpace(item))
            //{
            //    LogHelper.LogInfo(item);
            //    this.webBrowser.Navigate(new Uri(item, UriKind.Relative));

            //}

            string json = base.NavigationContext.QueryString["context"];
            if (string.IsNullOrWhiteSpace(json))
            {
                return;
            }

            GClass1 item = JSONHelper.Deserialize<GClass1>(Uri.UnescapeDataString(json));
            if (item == null)
                return;

            try
            {
                string filepath = string.Concat("content\\", item.Path, @"\", item.FileName);
                LogHelper.LogInfo(string.Format("文件路径：{0}", filepath));
                if (IsolatedStorageHelper.Instance.FileExists(filepath))
                {
                    if (item.IsImage)
                    {
                        this.imageInfo.Visibility = Visibility.Visible;
                        Stream stream = IsolatedStorageHelper.Instance.ReadFile(filepath);
                        BitmapImage bitmap = new BitmapImage();
                        bitmap.SetSource(stream);
                        this.imageInfo.Source = bitmap;
                    }
                    else if (item.IsTextFile)
                    {
                        this.txtInfo.Visibility = System.Windows.Visibility.Visible;
                        string content = string.Empty;
                        Stream stream = IsolatedStorageHelper.Instance.ReadFile(filepath);
                        if (stream == null)
                        {
                            Prompt.PromptMessage("读取文件内容为空！");
                            return;
                        }

                        FileHelper fileHelper = new FileHelper();
                        byte[] bytes = fileHelper.StreamToBytes(stream);
                        if (!(bytes != null && bytes.Length > 0))
                        {
                            Prompt.PromptMessage("读取文件内容为空！");
                            return;
                        }
                        Encoding encoding = fileHelper.GetEncodingTypeByStream(stream);
                        if (encoding == null)
                        {
                            content = new Gb2312Encoding().GetString(bytes, 0, bytes.Length);
                        }
                        else
                        {
                            content = encoding.GetString(bytes, 0, bytes.Length);
                        }

                        if (string.IsNullOrWhiteSpace(content))
                        {
                            Prompt.PromptMessage("文件内容为空！");
                        }
                        else
                        {
                            if (content.Length > 500)
                            {
                                Prompt.PromptMessage("文件较大，只读取部分内容进行预览！");
                                this.txtInfo.Text = content.Substring(0, 500);
                            }
                            else
                                this.txtInfo.Text = content;
                        }
                    }
                    else
                    {
                        this.webBrowser.Visibility = Visibility.Visible;
                        this.webBrowser.Navigate(new Uri(filepath, UriKind.Relative));
                    }
                }
                else
                {
                    Prompt.PromptMessage("文件未找到！");
                }
            }
            catch (Exception err)
            {
                Prompt.PromptMessage("预览文件内容出错了！！！"+err.Message);
            }
        }


    }
}